package com.yloveg.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yloveg.base.BaseController;
import com.yloveg.common.PageUtil;
import com.yloveg.entity.SPermission;
import com.yloveg.entity.SPermissionExtend;
import com.yloveg.entity.SRole;
import com.yloveg.entity.SRolePermission;
import com.yloveg.entity.SUserRole;
import com.yloveg.entity.Suser;
import com.yloveg.entity.UserInfo;
import com.yloveg.service.SPermissionService;
import com.yloveg.service.SRolePermissionService;
import com.yloveg.service.SRoleService;
import com.yloveg.service.SUserRoleService;
import com.yloveg.service.UserInfoService;
import com.yloveg.service.UserService;

import net.sf.json.JSONObject;


@Controller
@RequestMapping({"power"})  
public class PowerController extends BaseController {  

    @Autowired
    private UserService userService;
    
    @Autowired
    private SRoleService sRoleService;
    
    @Autowired
    private UserInfoService userInfoService;
    
    @Autowired
    private SUserRoleService sUserRoleService;
    
    @Autowired
    private SPermissionService sPermissionService;
    
    @Autowired
    private SRolePermissionService sRolePermissionService;
 
    
    /*********************************************角色START********************************************/
    /**
     * 角色列表
     * @return
     */
    @RequestMapping("/roleList")
    public String roleList(ModelMap map){
    	 return "landing/power/roleList";
    }
    @RequestMapping("/queryRolePage")
    @ResponseBody
    public Map<String, Object> queryRolePage(int page, int limit,SRole sRole){
        Map<String, Object> map = PageUtil.dateToLayTable(sRoleService.queryUserMsgPage(page,limit,sRole));
        return map;
    }
    @RequestMapping("/queryRoleMap")
    @ResponseBody
    public List<Map<String,Object>> queryRoleMap(SRole sRole){
        List<Map<String,Object>> result = sRoleService.queryUserMsgMap(sRole);
        return result;
    }
    /**
     * 角色列表
     * @return
     */
    @RequestMapping("/addRole")
    public String addRole(Long id,ModelMap map){
    	if(id!=null){
    		map.put("sRole", sRoleService.selectByPrimaryKey(id));
    		SRolePermission sRolePermission = new SRolePermission();
    		sRolePermission.setRoleId(id);
    		List<SRolePermission> list = sRolePermissionService.querySRolePermission(sRolePermission);
    		if(list.size()>0){
    			List<Long> permissionIds = new ArrayList<Long>();
        		for (SRolePermission sRolePer : list) {
        			permissionIds.add(sRolePer.getPermissionId());
    			}
        		map.put("permissionIds", permissionIds.toString());
    		}
    	}else{
    		map.put("sRole", new SRole());
    	}
    	return "landing/power/addRole";
    }    
    /**
     * 保存角色信息
	* @param info
	* @return
	*/
	@RequestMapping("/saveUpRole")
	@ResponseBody
	public Map<String, Object> saveUpRole(SRole sRole,String permissionId){
		String [] stringArr= permissionId.split(","); 
		Map<String, Object> map = new HashedMap<String, Object>();
		int result = 0;
		if(sRole.getId()==0) {
			result = sRoleService.insertSRole(sRole);
		}else {
			result = sRoleService.updateByPrimaryKeySelective(sRole);
		}
		SRolePermission sRolePermission = new SRolePermission();
		sRolePermission.setRoleId(sRole.getId());
		sRolePermissionService.deleteByTerm(sRolePermission);
		for (String idStr : stringArr) {
			sRolePermission.setPermissionId(Long.parseLong(idStr));
			sRolePermissionService.insertSRolePermission(sRolePermission);
		}
		if(result>0){
			map.put("code", "1");
			map.put("msg", "操作成功！");
		}else {
			map.put("code", "0");
			map.put("msg", "系统错误请联系管理员！");
		}
		return map;		
	}

    /**
     * 删除角色信息
	* @param info
	* @return
	*/
	@RequestMapping("/deleteRole")
	@ResponseBody
	public Map<String, Object> deleteRole (SRole sRole) {
		Map<String, Object> map = new HashedMap<String, Object>();
		int result = 0;
		if(sRole.getId()!=null) {
			sRole.setState(0);
			result = sRoleService.updateByPrimaryKeySelective(sRole);
		}
		if(result>0){
			map.put("code", "1");
			map.put("msg", "操作成功！");
		}else {
			map.put("code", "0");
			map.put("msg", "系统错误请联系管理员！");
		}
		return map;
	}
	/*********************************************角色END********************************************/
	
	/*********************************************用户信息START********************************************/
    /**
     * 用户列表
     * @return
     */
    @RequestMapping("/userList")
    public String userList(Long id,ModelMap map){
    	return "landing/power/userList";
    } 
    @RequestMapping("/queryUserPage")
    @ResponseBody
    public Map<String, Object> queryUserPage(int page, int limit,@RequestParam Map<String, Object> params){
        Map<String, Object> map = PageUtil.dateToLayTable(userService.queryUserPage(page, limit, params));
        return map;
    }
    
	/**
	 *修改账号状态
	* @param info
	* @return
	*/
	@RequestMapping("/changeUserState")
	@ResponseBody
	public Map<String, Object> changePwdSave (Suser user) {
		Map<String, Object> map = new HashedMap<String, Object>();
		if(userService.updateByPrimaryKeySelective(user)>0){
			map.put("code", "1");
			map.put("msg", "更改成功！");
		}else {
			map.put("code", "0");
			map.put("msg", "修改失败，请稍后再试。。。。");
		}
		return map;
	}
    /**
     * 用户分配角色（可多个角色）
     * @return
     */
    @RequestMapping("/userAddRole")
    public String userAddRole(Long id,ModelMap map){
    	UserInfo info = new UserInfo();
    	info.setUserId(id);
    	info = userInfoService.queryOneUserInfo(info);
    	if(info==null) {
    		map.put("info", new UserInfo());
    	}else {
    		map.put("info", info);	
    	}
    	map.put("Suser", userService.findUserById(id));
    	SUserRole sUserRole = new SUserRole();
    	sUserRole.setUserId(id);
    	List<SUserRole> sUserRoleList = sUserRoleService.querySRole(sUserRole);
    	if(sUserRoleList.size()>0){
    		List<Long> values = new ArrayList<Long>();
        	for (SUserRole sUserRole2 : sUserRoleList) {
        		values.add(sUserRole2.getRoleId());
    		}
        	map.put("values", values.toString());
    	}
    	
    	return "landing/power/userAddRole";
    }
    
	/**
	 *修改账号角色
	* @param info
	* @return
	*/
	@RequestMapping("/saveUserRole")
	@ResponseBody
	public Map<String, Object> saveUserRole (Long id,@RequestParam(value = "roleId[]", required = false) List<Long> roleId) {
		Map<String, Object> map = new HashedMap<String, Object>();
		SUserRole sUserRole = new SUserRole();
		sUserRole.setUserId(id);
		sUserRoleService.deleteByTerm(sUserRole);
		for (Long roleid : roleId) {
			sUserRole.setRoleId(roleid);
			sUserRoleService.insertSRole(sUserRole);
		}
		map.put("code", "1");
		map.put("msg", "设置成功！");
		return map;
	}
	/*********************************************用户信息END********************************************/
	
	/*********************************************资源START********************************************/
    /**
     * 资源列表
     * @return
     */
    @RequestMapping("/permissionList")
    public String permissionList(ModelMap map){
    	return "landing/power/permissionList";
    } 

    /**
     * 编辑添加资源
     * @return
     */
    @RequestMapping("/addPermission")
    public String addPermission(Long id,ModelMap map){
    	if(id!=null) {
    		map.put("sPermission", sPermissionService.selectByPrimaryKey(id));
    	}else {
    		map.put("sPermission", new SPermission());
    	}
    	return "landing/power/addPermission";
    } 
    
	/**
	 *保存资源
	* @param info
	* @return
	*/
	@RequestMapping("/savePermission")
	@ResponseBody
	public Map<String, Object> savePermission (SPermission sPermission) {
		Map<String, Object> map = new HashedMap<String, Object>();
		if(sPermission.getId()==0){
			sPermissionService.insertSPermission(sPermission);
		}else{
			sPermission.setState(1);
			sPermissionService.updateByPrimaryKeySelective(sPermission);
		}
		map.put("code", "1");
		map.put("msg", "保存成功！");
		return map;
	}
	
	@RequestMapping("/deletePermission")
	@ResponseBody
	public Map<String, Object> deletePermission (SPermission sPermission) {
		Map<String, Object> map = new HashedMap<String, Object>();
		sPermission.setState(0);
		int result = sPermissionService.updateByPrimaryKeySelective(sPermission);
		if(result>0) {
			map.put("code", "1");
			map.put("msg", "删除成功！");
		}else {
			map.put("code", "2");
			map.put("msg", "删除失败请稍后再试！");
		}

		return map;
	}
	
	/**
	 *查询菜单
	* @param info
	* @return
	*/
	@RequestMapping("/getMem")
	@ResponseBody
	public List<SPermissionExtend> getMem (SPermission sPermission) {
		sPermission.setFatherId(1100000L);
		List<SPermissionExtend> memList = sPermissionService.getMem(sPermission);
		for (SPermissionExtend sPermissionExtend : memList) {
			sPermission.setFatherId(sPermissionExtend.getId());
			List<SPermissionExtend> memList2 = sPermissionService.getMem(sPermission);
			for (SPermissionExtend sPermissionExtend2 : memList2) {
				sPermission.setFatherId(sPermissionExtend2.getId());
				List<SPermissionExtend> memList3 = sPermissionService.getMem(sPermission);
				sPermissionExtend2.setChildren(memList3);
			}
			sPermissionExtend.setChildren(memList2);
		}
		return memList;
	}
	/**
	 *查询权限所需菜单
	* @param info
	* @return
	*/
	@RequestMapping("/getMemRole")
	@ResponseBody
	public String getMemRole (SPermission sPermission) {
		sPermission.setFatherId(1100000L);
		JSONObject roleJson = new JSONObject();
		List<SPermissionExtend> memList = sPermissionService.getMem(sPermission);
		for (SPermissionExtend sPermissionExtend : memList) {
			sPermission.setFatherId(sPermissionExtend.getId());
			List<SPermissionExtend> memList2 = sPermissionService.getMem(sPermission);
			for (SPermissionExtend sPermissionExtend2 : memList2) {
				sPermission.setFatherId(sPermissionExtend2.getId());
				List<SPermissionExtend> memList3 = sPermissionService.getMem(sPermission);
				sPermissionExtend2.setChildren(memList3);
			}
			sPermissionExtend.setChildren(memList2);
		}
		roleJson.put("code", 0);
		roleJson.put("data", memList);
		return roleJson.toString();
	}
	/*********************************************资源END********************************************/
	/*********************************************根据用户获取菜单START********************************************/
	@RequestMapping("/getUserMem")
	@ResponseBody
	public Map<Long,List<SPermissionExtend>> getUserMem () {
		return sPermissionService.getUserMainMumEche(this.getSessionUser().getId());
	}	
	
	
	/*********************************************根据用户获取菜单END********************************************/

}
